const CopyWebpackPlugin = require('copy-webpack-plugin')
const path = require('path')
const fs = require('fs')
const Uglify = require('uglify-js')
var nodeModules = {}
// 过滤node_modules中的所有模块
fs.readdirSync('node_modules')
  .filter(function (x) {
    if (x === 'jwt-simple') return false
    return ['.bin'].indexOf(x) === -1
  })
  .forEach(function (mod) {
    nodeModules[mod] = 'commonjs ' + mod
  })
module.exports = {
  mode: 'production',
  target: 'electron-main',
  entry: path.resolve('src/main/index.ts'),
  output: {
    path: path.resolve('dist/electron/main'),
    filename: 'index.js'
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/
      }
    ]
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js']
  },
  externals: nodeModules,
  plugins: [
    new CopyWebpackPlugin([
      {
        from: 'src/index.js',
        to: '../',
        transform: content => {
          return Uglify.minify(content.toString()).code
        }
      },
      {
        from: 'src/renderer.js',
        to: '../',
        transform: content => {
          return Uglify.minify(content.toString()).code
        }
      }
    ])
  ]
}
